package com.info.zhiduoduo.repository.dto.entrustmentproject;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.info.zhiduoduo.common.utils.DateUtil;

import org.springframework.beans.BeanUtils;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class ProjectEntrusteeInfoDto {

	@ApiModelProperty("批量导入id")
	private Integer importId;
	@ApiModelProperty("企业id")
	private Integer compid;
	@ApiModelProperty("企业名称")
	private String compname;
	@ApiModelProperty("委托人主体id")
	private Integer principalId;
	@ApiModelProperty("委托人主体名称")
	private String principalName;

	@ApiModelProperty("受托方id, 即创客id")
	private Integer entrusteeId;
	@ApiModelProperty("创客姓名")
	private String entrusteeName;
	@ApiModelProperty("创客工作室名称")
	private String entrusteeStudio;

	@ApiModelProperty("项目类型 参考 ProjectType枚举类")
	private Integer projectType;
	@ApiModelProperty("项目名")
	private String name;
	@ApiModelProperty("项目内容")
	private String content;
	@ApiModelProperty("发票类型 1:普票 2:专票")
	private Integer invoiceType;
	@ApiModelProperty("发票内容")
	private String invoiceContent;
	@JsonFormat(pattern = DateUtil.FORMAT_STYLE_2, timezone = DateUtil.TIME_ZONE_CHINA)
	@ApiModelProperty("发单日期")
	private Date createtime;

	@ApiModelProperty("项目金额(导入时的金额),单位元")
	private BigDecimal projectAmount;
	@ApiModelProperty("实发金额,单位元")
	private BigDecimal amount;
	@ApiModelProperty("服务费 = 基础服务费 + 合伙人项目服务费")
	private BigDecimal serviceCharge;
	@ApiModelProperty("税费,创客团项目独有")
	private BigDecimal taxFee;
	@ApiModelProperty("服务费+税费,单位元")
	private BigDecimal serviceTaxAmount;
	@ApiModelProperty("含税金额(实发金额+服务费+税费),单位元")
	private BigDecimal taxInclusiveAmount;
	@ApiModelProperty("创客团-智众包项目,合伙人智众包佣金; 创客团- 项目:无")
	private BigDecimal partnerBrokerage;
	@ApiModelProperty("创客团-智众包项目,保证金金额; 创客团- 项目:无")
	private BigDecimal depositAmount;
	@ApiModelProperty("开票金额( 项目=含税金额，智众包项目=含税金额-保证金金额)")
	private BigDecimal invoiceAmount;
	@ApiModelProperty("企业应付金额(创客团智众包=含税金额+保证金金额, 其他=含税金额)")
	private BigDecimal companyPayAmount;

	@ApiModelProperty("包含的子项目列表")
	private List<EntrustmentProjectItemDto> itemList;

	@ApiModelProperty("合同编号")
	private String contractNo;

	@ApiModelProperty("主体id")
	private Integer partyId;
	@ApiModelProperty("主体名称")
	private String partyName;

	public static ProjectEntrusteeInfoDto of(EntrustmentProjectItemDto item) {
		ProjectEntrusteeInfoDto info = new ProjectEntrusteeInfoDto();
		BeanUtils.copyProperties(item, info);
		return info;
	}

	public static Map<String, Object> ofMap(ProjectEntrusteeInfoDto dto) {
		LocalDate localDate = DateUtil.date2LocalDate(dto.getCreatetime()).plusYears(1).minusDays(1);
		Date endDate = DateUtil.localDate2Date(localDate);
		Map<String, Object> data = new HashMap<>();
		data.put("contractNo", dto.getContractNo());
		data.put("compname", dto.getCompname());
		data.put("partyName", dto.getPartyName());
		data.put("username", dto.getEntrusteeStudio());
		data.put("serviceCharge", dto.getProjectAmount());
		data.put("contractDate", DateUtil.getString(dto.getCreatetime(), DateUtil.FORMAT_STYLE_3));
		data.put("validity", 1);
		data.put("startYear", DateUtil.getString(dto.getCreatetime(), "yyyy"));
		data.put("startMonth", DateUtil.getString(dto.getCreatetime(), "MM"));
		data.put("startDay", DateUtil.getString(dto.getCreatetime(), "dd"));
		data.put("endYear", DateUtil.getString(endDate, "yyyy"));
		data.put("endMonth", DateUtil.getString(endDate, "MM"));
		data.put("endDay", DateUtil.getString(endDate, "dd"));
		data.put("projectname", dto.getName());
		data.put("content", dto.getContent());
		return data;
	}

	public ProjectEntrusteeInfoDto add(ProjectEntrusteeInfoDto info) {
		this.projectAmount = this.projectAmount.add(info.getProjectAmount());
		this.amount = this.amount.add(info.getAmount());
		this.serviceCharge = this.serviceCharge.add(info.getServiceCharge());
		this.taxFee = this.taxFee.add(info.getTaxFee());
		this.serviceTaxAmount = this.serviceTaxAmount.add(info.getServiceTaxAmount());
		this.taxInclusiveAmount = this.taxInclusiveAmount.add(info.getTaxInclusiveAmount());
		this.partnerBrokerage = this.partnerBrokerage.add(info.getPartnerBrokerage());
		this.depositAmount = this.depositAmount.add(info.getDepositAmount());
		this.invoiceAmount = this.invoiceAmount.add(info.getInvoiceAmount());
		this.companyPayAmount = this.companyPayAmount.add(info.getCompanyPayAmount());
		return this;
	}
}
